import { useReducer } from 'react';
import { for_obj, obj_str } from 'sui';

const reducer = (status, action) => {
    const ret = { ...status };
    for_obj(action, (k, v) => ret[k] = v);

    return ret;
};

const useStep = () => {
    const [status, dispatch] = useReducer(reducer, {});

    const hset = (k, v) => {
        const d = {};
        d[k] = v;

        dispatch(d);
    };

    const hget = (k) => {
        return obj_str(status, k);
    };

    const hclear = () => {
        const ret = { ...status };

        for_obj(ret, (k, v) => ret[k] = "");

        dispatch(ret);
    };

    return {
        get: hget,
        mset: v => dispatch(v),
        set: hset,
        clear: hclear,

        setErr: v => hset("err", v),
    };
};

export {useStep};
